package com.test.prolenders.adapter_prolenders;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.prolenders.R;

import java.util.ArrayList;

public class LoginCarouselAdapter_prolenders extends RecyclerView.Adapter<LoginCarouselAdapter_prolenders.LoginCarouselHolder> {

    private final ArrayList<LoginCarouselInfo> infos = new ArrayList<>();

    public LoginCarouselAdapter_prolenders(Context context){
        infos.add(new LoginCarouselInfo(
                context.getText(R.string.login_carousel_1_title_prolenders),
                context.getText(R.string.login_carousel_1_text_prolenders),
                R.drawable.bg_login_carousel_1_prolenders,
                R.drawable.img_login_carousel_1_prolenders
        ));
        infos.add(new LoginCarouselInfo(
                context.getText(R.string.login_carousel_2_title_prolenders),
                context.getText(R.string.login_carousel_2_text_prolenders),
                R.drawable.bg_login_carousel_2_prolenders,
                R.drawable.img_login_carousel_2_prolenders
        ));
        infos.add(new LoginCarouselInfo(
                context.getText(R.string.login_carousel_3_title_prolenders),
                context.getText(R.string.login_carousel_3_text_prolenders),
                R.drawable.bg_login_carousel_3_prolenders,
                R.drawable.img_login_carousel_3_prolenders
        ));
    }

    @NonNull
    @Override
    public LoginCarouselHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LoginCarouselHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_login_carousel_prolenders,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull LoginCarouselHolder holder, int position) {

        int index=position%infos.size();

        LoginCarouselInfo info = infos.get(index);

        holder.setTitle(info.getTitle());
        holder.setText(info.getText());
        holder.setBg(info.getBgRes());
        holder.setImg(info.getImgRes());
        holder.setPointView(index,infos.size());

    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public static class LoginCarouselHolder extends RecyclerView.ViewHolder{

        private final View mView;

        private final View bgView;
        private final ImageView imgIv;
        private final  TextView titleTv;
        private final  TextView textTv;
        private final LinearLayout pointgroupLayout;

        @SuppressLint("InflateParams")
        public LoginCarouselHolder(View view){

            super(view);
            mView = view;

            bgView = mView.findViewById(R.id.ilc_layout_bg);
            imgIv = view.findViewById(R.id.ilc_iv_img);
            titleTv = mView.findViewById(R.id.ilc_tv_title);
            textTv = mView.findViewById(R.id.ilc_tv_text);
            pointgroupLayout = mView.findViewById(R.id.ilc_layout_point_group);

        }

        public void setImg(int imgResId){
            imgIv.setImageResource(imgResId);
        }

        public void setTitle(CharSequence title) {
            titleTv.setText(title);
        }

        public void setText(CharSequence text){
            textTv.setText(text);
        }

        public void setBg(int bgRedId){
            bgView.setBackgroundResource(bgRedId);
        }

        public void setPointView(int pointCount,int max){
            pointgroupLayout.removeAllViews();
            for (int i=0;i<max;i++){
                @SuppressLint("InflateParams") View pointView = LayoutInflater.from(mView.getContext()).inflate(R.layout.view_blank_prolenders,null);
                pointView.setBackgroundResource(i==pointCount ? R.drawable.bg_login_carousel_point_true_prolenders :R.drawable.bg_login_carousel_point_false_prolenders);
                pointgroupLayout.addView(pointView);
            }
        }

    }

    private static class LoginCarouselInfo{

        private final CharSequence title;
        private final CharSequence text;
        private final int bgRes;
        private final int imgRes;

        public LoginCarouselInfo(CharSequence title, CharSequence text, int bgRes, int imgRes) {
            this.title = title;
            this.text = text;
            this.bgRes = bgRes;
            this.imgRes = imgRes;
        }

        public CharSequence getTitle() {
            return title;
        }

        public CharSequence getText() {
            return text;
        }

        public int getBgRes() {
            return bgRes;
        }

        public int getImgRes() {
            return imgRes;
        }
    }
}
